package com.lightjet.macross.base.upms.controller;

import com.lightjet.macross.base.feign.api.controller.PrincipalController;
import com.lightjet.macross.base.upms.domain.SysUser;
import com.lightjet.macross.base.upms.domain.SystemInfo;
import com.lightjet.macross.base.upms.service.SystemInfoService;
import com.lightjet.macross.common.dto.ResponseDTO;
import com.lightjet.macross.common.util.RespUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.java.Log;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.MessageFormat;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author : lijia
 * @version : 1.0 2020-03-10 10:56
 * description : 接入应用系统控制器
 */

@RestController
@RequestMapping("/api/system")
@Scope("request")
@Log
@RefreshScope
@Api(tags="通用系统权限管理-租户系统相关接口集")
public class SystemController extends PrincipalController {

    @Resource
    private SystemInfoService systemInfoService;

    @GetMapping(value = "/all")
    @ApiOperation(value = "租户系统查询接口", notes = "获取所有系统")
    public ResponseDTO<List<SystemInfo>> GetAll()
    {
        return RespUtil.CreateOK(systemInfoService.FindAll().stream().sorted(Comparator.comparing(SystemInfo::getId)).collect(Collectors.toList()));
    }

    @PostMapping(value="",consumes = "application/json")
    @ApiOperation(value = "租户系统新增接口", notes = "根据租户系统信息Json新增")
    public ResponseDTO<SystemInfo> CreateSystem(
            @ApiParam(name = "systemInfo",
                    value = "租户系统实体类Json",
                    type="body") @RequestBody SystemInfo systemInfo)
    {
        return RespUtil.CreateOK(systemInfoService.CreateAndFlush(systemInfo));
    }

    @PutMapping(value="",consumes = "application/json")
    @ApiOperation(value = "租户系统新增接口", notes = "根据租户系统信息Json新增")
    public ResponseDTO<SystemInfo> UpdateSystem(
            @ApiParam(name = "systemInfo",
                    value = "租户系统实体类Json",
                    type="body") @RequestBody SystemInfo systemInfo)
    {
        SystemInfo original = systemInfoService.FindDistinctById(systemInfo.getId());
        original.setUsibn(systemInfo.getUsibn());
        original.setSystemdescription(systemInfo.getSystemdescription());
        original.setSystemname(systemInfo.getSystemname());
        original.setShortname(systemInfo.getShortname());
        original.setRemark(systemInfo.getRemark());
        return RespUtil.CreateOK(systemInfoService.UpdateAndFlush(original));
    }

    @DeleteMapping(value = "/batch",consumes = "application/json")
    @ApiOperation(value = "租户系统批量删除接口", notes = "根据租户系统ID数组ListJson批量删除")
    public ResponseDTO<String> DeleteBatch(@ApiParam(name = "ids",value = " List Json形式",type="body") @RequestBody List<Integer> ids)
    {
        StringBuilder sb = new StringBuilder();
        for(Integer id : ids)
        {
            try
            {
                systemInfoService.DeleteOne(new SystemInfo(id));
            }
            catch (Exception e)
            {
                sb.append(MessageFormat.format("删除租户系统【{0}】删除失败：{1};",id,e.getMessage()));
            }
        }
        if(sb.length()>0)
        {
            return RespUtil.CreateError(sb.toString());
        }
        else
        {
            return RespUtil.CreateOK();
        }
    }

}
